﻿#include "frmpngtool.h"
#include "ui_frmpngtool.h"
#include "qfile.h"
#include "qdesktopwidget.h"
#include "qfiledialog.h"
#include "qdebug.h"
#include "flatui.h"

#include <qdatetime.h>
#include <qprocess.h>
#include <quuid.h>

frmPngTool::frmPngTool(QWidget *parent) : QWidget(parent), ui(new Ui::frmPngTool)
{

    this->initForm();

    mainDir = QCoreApplication::applicationDirPath();
}

frmPngTool::~frmPngTool()
{
    delete ui;
}

void frmPngTool::initForm()
{
     ui->setupUi(this);
     ui->progress->setRange(0, 0);
     ui->progress->setValue(0);
     ui->textBrowser->setTextColor(QColor("#FFFFFF"));
     ui->textBrowser->setStyleSheet("background-color: black;");

     ui->radioButton->click();

     connect(ui->txtUrls, SIGNAL(returnPressed()), this, SLOT(saveUrls()));//连接信号与槽
     connect(ui->radioButton, SIGNAL(clicked()), this, SLOT(radioButtonClk()));
     connect(ui->radioButton_2, SIGNAL(clicked()), this, SLOT(radioButton2Clk()));

     this->setStyleSheet("*{outline:0px;}QWidget#frmFlatUI{background:#FFFFFF;}");

     FlatUI::setLineEditQss(ui->outDirText, 3, 1, "#DCE4EC", "#3498DB");
     FlatUI::setLineEditQss(ui->txtDir, 3, 1, "#DCE4EC", "#3498DB");
     FlatUI::setLineEditQss(ui->txtUrls, 3, 1, "#DCE4EC", "#3498DB");
     FlatUI::setPushButtonQss(ui->btnDir, 3, 4, "#3498DB", "#FFFFFF", "#5DACE4", "#E5FEFF", "#2483C7", "#A0DAFB");
     FlatUI::setPushButtonQss(ui->outDirBtn, 3, 4, "#3498DB", "#FFFFFF", "#5DACE4", "#E5FEFF", "#2483C7", "#A0DAFB");
     FlatUI::setPushButtonQss(ui->btnOk, 3, 4, "#3498DB", "#FFFFFF", "#5DACE4", "#E5FEFF", "#2483C7", "#A0DAFB");
     FlatUI::setProgressQss(ui->progress, 8, 5, 9, "#E8EDF2", "#1ABC9C");
     FlatUI::setRadioButtonQss(ui->radioButton, 8, "#D7DBDE", "#3498DB");
     FlatUI::setRadioButtonQss(ui->radioButton_2, 8, "#D7DBDE", "#3498DB");

     //设置列数和列宽
     int width = qApp->desktop()->availableGeometry().width() - 120;
     ui->tableWidget->setColumnCount(2);
     ui->tableWidget->setColumnWidth(0, width * 0.02);
     ui->tableWidget->setColumnWidth(1, width * 0.16);
     ui->tableWidget->verticalHeader()->setDefaultSectionSize(25);

     QStringList headText;
     headText  << "编号" << "URL";
     ui->tableWidget->setHorizontalHeaderLabels(headText);
     ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
     ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
     ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
     ui->tableWidget->setAlternatingRowColors(true);
     ui->tableWidget->verticalHeader()->setVisible(false);
     ui->tableWidget->horizontalHeader()->setStretchLastSection(true);

}

void frmPngTool::saveUrls()
{
    QString s = ui->txtUrls->text();
    urlStrs.append(s);
    //设置行高
    ui->tableWidget->setRowCount(k + 1);
    ui->tableWidget->setRowHeight(k, 24);
    ui->tableWidget->setItem(k, 0, new QTableWidgetItem(QString::number(k + 1)));
    ui->tableWidget->setItem(k, 1, new QTableWidgetItem(s));
    k++;
}


void frmPngTool::radioButtonClk()
{
    transtType = "pdf";
    transtTypeExt = "pdf";
}


void frmPngTool::radioButton2Clk()
{
    transtType = "image";
    transtTypeExt = "png";
}

void frmPngTool::on_btnDir_clicked()
{
    QString dir = QFileDialog::getExistingDirectory(this, "选择目录");
    if (!dir.isEmpty()) {
        ui->txtDir->setText(dir);
        ui->progress->setValue(0);
    }
}

void frmPngTool::on_outDirBtn_clicked()
{
    outDir = QFileDialog::getExistingDirectory(this, "输出目录");
    if (!outDir.isEmpty()) {
        ui->outDirText->setText(outDir);
        ui->progress->setValue(0);
    }
}

void frmPngTool::on_btnOk_clicked()
{

    if(outDir.isEmpty()){
        QMessageBox::critical(0 , "error" , "请选择输出目录！", QMessageBox::Ok | QMessageBox::Default , QMessageBox::Cancel | QMessageBox::Escape , 	0 );

    } else if(files.isEmpty() && ui->outDirText->text().isEmpty()){
        QMessageBox::critical(0 , "error" , "请选择要转换的文件！", QMessageBox::Ok | QMessageBox::Default , QMessageBox::Cancel | QMessageBox::Escape , 	0 );

    }
    else{
        files.clear();

        //将该目录下的所有html文件存入链表
        QString currentDir = ui->txtDir->text().trimmed();
        if (!currentDir.isEmpty()) {
            QDir imagePath(currentDir);
            QStringList filter;
            filter << "*.html";
            QStringList list = imagePath.entryList(filter);

            foreach (QString str, list) {
                files.append(currentDir + "/" + str);
            }
        }

        ui->progress->setRange(0, files.count());
        ui->progress->setValue(0);

        int count = 0;

        foreach (QString urlStr, urlStrs) {

            QProcess p(0);

            QString cmdStr = "";
            cmdStr.append(mainDir);
            cmdStr.append("/wkhtmltopdf/bin/wkhtmlto" + transtType + ".exe");
            cmdStr.append(" ");
            cmdStr.append(urlStr);
            cmdStr.append(" ");

            QDateTime current_date_time =QDateTime::currentDateTime();
            QString current_date =current_date_time.toString("yyyyMMddhhmmss");

            cmdStr.append(outDir + "/" + QUuid::createUuid().toString()+ current_date + "." + transtTypeExt);
            cmdStr.append(" ");

            p.start(cmdStr);
            p.waitForStarted();
            p.waitForFinished();

            ui->textBrowser->append(QString::fromLocal8Bit(p.readAllStandardError()));

            count++;
            ui->progress->setValue(count);

            qApp->processEvents();
        }

        foreach (QString file, files) {

            //ui->textBrowser->append("current file:" + file);

            QProcess p(0);

            QString cmdStr = "";
            cmdStr.append(mainDir);
            cmdStr.append("/wkhtmltopdf/bin/wkhtmlto" + transtType + ".exe");
            cmdStr.append(" ");
            cmdStr.append(file);
            cmdStr.append(" ");

            QString pattern(".html$");
            QRegExp rx(pattern);
            QString newStr = file.replace(rx,"." + transtTypeExt);

            cmdStr.append(ui->outDirText->text());

            cmdStr.append(newStr.mid(file.lastIndexOf("/")));

            p.start(cmdStr);
            p.waitForStarted();
            p.waitForFinished();

            ui->textBrowser->append(cmdStr);

            ui->textBrowser->append(QString::fromLocal8Bit(p.readAllStandardError()));

            count++;
            ui->progress->setValue(count);
            qApp->processEvents();
        }

        ui->textBrowser->append("finsh");
        qDebug() << "finsh";
    }
}
